package net.xxcl.stuManager.mapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import net.xxcl.stuManager.pojo.Poor;
import net.xxcl.stuManager.pojo.Student;
import net.xxcl.stuManager.util.DigestUtil;

public class StudentMapper {
	
	/**
	 * 学生登录验证
	 * @param card
	 * @param password
	 * @return
	 */
	public List<Student> login(String card,String password) {
		SqlSession session = DBSessionFactory.getSqlSessionFactory().openSession();
		Map<String, String> map = new HashMap<String, String>();
		map.put("card", card);
		map.put("password", password);
		try {
			return session.selectList("net.xxcl.stuManager.mapper.StudentMapper.login",map);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 辅导员可管理的学生列表
	 * @return
	 */
	public List<Student> selectListByTeacherid(Map<String, Object> map) {
		SqlSession session = DBSessionFactory.getSqlSessionFactory().openSession();
		return session.selectList("net.xxcl.stuManager.mapper.StudentMapper.selectListByTeacherid",map);
	}
	
	
	
	
	/**
	 * 辅导员可管理的学生总数
	 * @return
	 */
	public int countByTeacherid(Map<String, Object> map) {
		SqlSession session = DBSessionFactory.getSqlSessionFactory().openSession();
		return session.selectOne("net.xxcl.stuManager.mapper.StudentMapper.countByTeacherid",map);
	}
	
	/**
	 * 新增学生
	 * @param student
	 * @return
	 */
	public int save(Student student) {
		String card = student.getCard();
		//取出身份证号后6位作为初始密码
		String password = card.substring(card.length()-6);System.out.println(password);
		student.setPassword(DigestUtil.encodeSHA512Hex(password.getBytes()));
		//从身份证号倒数第二位判断性别
		int sex = card.charAt(16)-'0';
		if(sex%2==0){
			student.setSex(false);
		} else {
			student.setSex(true);
		}
		SqlSession session = DBSessionFactory.getSqlSessionFactory().openSession();
		int count = session.insert("net.xxcl.stuManager.mapper.StudentMapper.save",student);
		session.commit();
		return count;
	}
	
	/**
	 * 根据id查找
	 * @param id
	 * @return
	 */
	public Student selectById(Integer id) {
		SqlSession session = DBSessionFactory.getSqlSessionFactory().openSession();
		return session.selectOne("net.xxcl.stuManager.mapper.StudentMapper.selectById",id);
	}
	
	/**
	 * 更新
	 * @param student
	 * @return
	 */
	public int update(Student student) {
		String card = student.getCard();
		//从身份证号倒数第二位判断性别
		int sex = card.charAt(16)-'0';
		if(sex%2==0){
			student.setSex(false);
		} else {
			student.setSex(true);
		}
		SqlSession session = DBSessionFactory.getSqlSessionFactory().openSession();
		int count = session.update("net.xxcl.stuManager.mapper.StudentMapper.update",student);
		session.commit();
		return count;
	}
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	public int deleteById(Integer id) {
		SqlSession session = DBSessionFactory.getSqlSessionFactory().openSession();
		int count = session.delete("net.xxcl.stuManager.mapper.StudentMapper.deleteByid",id);
		session.commit();
		return count;
	}
	/**
	 * 根据id重置学生密码
	 * @param id
	 * @return
	 */
	public int restByid(Integer id) {
		SqlSession session = DBSessionFactory.getSqlSessionFactory().openSession();
		int count = session.update("net.xxcl.stuManager.mapper.StudentMapper.restByid",id);
		session.commit();
		return count;
	}
}
